package com.powernode.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.constant.ProductConstants;
import com.powernode.domain.ProdProp;
import com.powernode.domain.ProdPropValue;
import com.powernode.mapper.ProdPropMapper;
import com.powernode.mapper.ProdPropValueMapper;
import com.powernode.service.ProdPropService;
import com.powernode.service.ProdPropValueService;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.lang.invoke.LambdaMetafactory;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@CacheConfig(cacheNames = "com.powernode.service.impl.ProdPropServiceImpl")

public class ProdPropServiceImpl extends ServiceImpl<ProdPropMapper, ProdProp> implements ProdPropService {

    @Autowired
    private ProdPropMapper prodPropMapper;

    @Autowired
    private ProdPropValueMapper prodPropValueMapper;

    @Autowired
    private ProdPropValueService prodPropValueService;

    @Override
    public Page<ProdProp> queryProdSpecPage(Long current, Long size, String propName) {
//        多条件分页查询商品属性
        Page<ProdProp> page = new Page<>(current, size);
        page = prodPropMapper.selectPage(page, new LambdaQueryWrapper<ProdProp>()
                .like(StringUtils.hasText(propName), ProdProp::getPropName, propName));
        List<ProdProp> prodPropList = page.getRecords();
        if (CollectionUtil.isEmpty(prodPropList) || prodPropList.size() == 0) {
            return page;
        }
//        从属性对象集合中获取属性id集合，
        List<Long> propIdList = prodPropList.stream().map(ProdProp::getPropId).collect(Collectors.toList());
//        根据属性id集合查询属性值对象集合
        List<ProdPropValue> prodPropValuesList = prodPropValueMapper.selectList(new LambdaQueryWrapper<ProdPropValue>()
                .in(ProdPropValue::getPropId, propIdList));
//        循环遍历属性对象集合
        prodPropList.forEach(prodProp -> {
//            从属性值对象集合中过滤出于当前属性对象的属性id一致的对象集合
            List<ProdPropValue> propValues = prodPropValuesList.stream()
                    .filter(prodPropValue -> prodPropValue.getPropId()
                            .equals(prodProp.getPropId())).collect(Collectors.toList());
            prodProp.setProdPropValues(propValues);
        });
        return page;
    }


    /**
     * 1. 新增商品属性对象
     * 2. 批量新增商品属性值对象集合
     *
     * @param prodProp 商品规格
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(key = ProductConstants.PROD_PROP_KEY)
    public Boolean saveProdSpec(ProdProp prodProp) {
        prodProp.setShopId(1L);
        prodProp.setRule(2);
        int insert = prodPropMapper.insert(prodProp);
        if (insert > 0) {
            Long propId = prodProp.getPropId();
//            新增商品属性值对象集合
            List<ProdPropValue> prodPropValues = prodProp.getProdPropValues();
            if (CollectionUtil.isNotEmpty(prodPropValues) && prodPropValues.size() != 0) {
                prodPropValues.forEach(prodPropValue -> prodPropValue.setPropId(propId));
                prodPropValueService.saveBatch(prodPropValues);
            }
        }
        return insert > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(key = ProductConstants.PROD_PROP_KEY)
    public Boolean modifyProdSpec(ProdProp prodProp) {
//        获取新的属性值对象集合
        List<ProdPropValue> newProdPropValues = prodProp.getProdPropValues();
//        批量修改属性值对象
        boolean b = prodPropValueService.updateBatchById(newProdPropValues);
        if (b) {
            //修改商品属性
            prodPropMapper.updateById(prodProp);
        }
        return b;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(key = ProductConstants.PROD_PROP_KEY)
    public Boolean removeProdSpec(Long propId) {
//        根据属性表示删除属性值
        prodPropValueMapper.delete(new LambdaQueryWrapper<ProdPropValue>()
                .eq(ProdPropValue::getPropId, propId));
        return prodPropMapper.deleteById(propId) > 0;
    }

    @Override
    @Cacheable(key = ProductConstants.PROD_PROP_KEY)
    public List<ProdProp> queryProdPropList() {
        return prodPropMapper.selectList(null);
    }
}
